package suanfarumen;


class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }
}
public class day10 {
    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode i=list1;
        ListNode j=list2;
        if(i==null) return list2;
        if(j==null) return list1;
        if(i.val>j.val) { //i指向小的
            i=list2;
            j=list1;
        }
        while(j!=null){
            if(i.next==null||i.next.val>=j.val){
                ListNode tj=j.next;
                j.next=i.next;
                i.next=j;
                j=tj;
                i=i.next;
            }else{
                i=i.next;
            }
        }
        return list1.val<=list2.val?list1:list2;
    }

    public static void main(String[] args) {
        ListNode list1=new ListNode(2,null);
        ListNode list2=new ListNode(1,null);
        System.out.println(mergeTwoLists(list1,list2));
    }
}
